package com.joshondesign.sdljava.demo;

import com.joshondesign.sdljava.*;
import com.joshondesign.sdljava.gfx.Util;

import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: josh
 * Date: 9/14/11
 * Time: 4:21 AM
 * To change this template use File | Settings | File Templates.
 */
public class AudioTest implements SDLUtil.InitCallback {
    public static void main(String ... args) {
        SDLUtil.init(new AudioTest());
    }

    public void callback() throws Exception {
        //Util.standard_init();

        if(SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDLConstants.SDL_INIT_TIMER | SDLConstants.SDL_INIT_AUDIO) < 0) {
            throw new SDLException("init failed");
        }
        SDLUtil.dumpInfo();



        Util.p("opening audio");
        if(SDL.Mix_OpenAudio(
                44100,//SDLConstants.MIX_DEFAULT_FREQUENCY,
                SDLConstants.MIX_DEFAULT_FORMAT,
                2,
                4096)<0) {
            Util.p("opening audio error: ");
            throw new Exception("error");

        }


        int num_music_decoders = SDL.Mix_GetNumMusicDecoders();
        Util.p("num music decoders: " + num_music_decoders);
        for(int i=0; i<num_music_decoders; i++) {
            String name = SDL.Mix_GetMusicDecoder(i);
            Util.p("    decoder: " + name);
        }



        //int initted = SDL.Mix_Init(0);
        //Util.p("initted = " + initted + " " + Integer.toBinaryString(initted));



        Util.p("loading music");
        File file = new File("test2.wav");
        Util.p("exists = " + file.exists());
        SWIGTYPE_p__Mix_Music music = SDL.Mix_LoadMUS(file.getAbsolutePath());
        Util.p("loaded music: " + music);
        //install callback
        //in a loop get the most recent sample
        SDL.doFunky();

        SDL.Mix_PlayMusic(music, 0);
        //SDL.Mix_HookMusicFinished(); need a callback hook of some sort

        Thread.currentThread().sleep(4000);
        SDL.Mix_CloseAudio();
        SDL.SDL_Quit();
    }
}
